package com.qfedu.edu.iotserver.pay.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author xiaobobo
 * @title: OrderNumberUtils
 * @projectName payment
 * @description: 假设你们要生成订单 你们怎么生成呢?
 * @date 2022/8/31  9:22
 */
public class OrderNumberUtils {


    private static AtomicInteger atomicInteger=new AtomicInteger();

    /**
     * 生成订单
     *
     * 策略：
     *    年月日时分秒
     *    加上一个原子性的id值
     *
     *
     * @return
     */
    public static String getOrderNum(String id){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmsss");
        String format = simpleDateFormat.format(new Date());
        int auto = atomicInteger.incrementAndGet();
        //你可以来一个用户id值的HashCode值
        int idHashCode = id.hashCode();
        return format+"-"+auto+"-"+idHashCode;
    }


    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i <100 ; i++) {
           new Thread(new Runnable() {
               @Override
               public void run() {
                   String orderNum = getOrderNum("xiaobobo");
                   System.out.println("订单号是:"+orderNum);
               }
           }).start();
        }


        Thread.sleep(Integer.MAX_VALUE);
    }


}
